﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using CQIE.TES.IService;
using CQIE.TES.Model;
using Microsoft.Ajax.Utilities;
using Newtonsoft.Json.Linq;

namespace CQIE.TES.WEB.ADMIN.Controllers.API
{
    public class StudentAPIController : ApiController
    {
        public IStudentService StudentService { get; set; }

        //获取所有学生信息，分页
        [HttpGet]
        [Route("api/getallstudent")]
        public object GetStudentInfo(int page, int limit, string search)
        {
            if (search == null)
            {
                search = "";
            }

            int count;
            List<Student> list = StudentService.GetStudentsPage(page, limit, search, out count);


            var data = new
            {
                code = 0,
                count,
                msg = "success",
                data = list.Select(stu => new
                {
                    id = stu.ID,
                    studentName = stu.Name,
                    studentNumber = stu.StudentNumber,
                    classID = stu.ClassID,
                    classCode = stu.ClassCode,
                    sex = stu.Sex == 0 ? "女" : "男",
                    state = stu.IsFreeze == true ? "禁用" : "启用",
                })
            };

            return data;
        }


        //逻辑删除，通过禁用
        [HttpGet]
        [Route("api/deletestudent")]
        public bool DeleteStudent(int id)
        {
            return StudentService.DeleteStudent(id);
        }

        //学生查询
        [HttpGet]
        [Route("api/searchstudent")]
        [Obsolete]
        public object SearchStudent(string nameSearch)
        {
            //不输入值的情况
            if (nameSearch == null)
            {
                nameSearch = "";
            }
            List<Student> list = StudentService.SearchStudent(nameSearch);

            var data = new
            {
                code = 0,
                msg = "success",
                data = list.Select(stu => new
                {
                    id = stu.ID,
                    studentNumber = stu.StudentNumber,
                    studentName = stu.Name,
                    sex = stu.Sex == 0 ? "女" : "男",
                    classID = stu.ClassID,
                    // classCode = stu.ClassCode,
                    state = stu.IsFreeze,
                })
            };
            return data;
        }

        //添加学生
        [HttpPost]
        [Route("api/updatestudent")]
        public void UpdateStudent([FromBody] JObject data)
        {
            StudentService.UpdateStudent(data);

        }
        //重置学生密码
        [HttpGet]
        [Route("api/resetpassword")]
        public bool ResetPassWord(int id)
        {
            try
            {
                StudentService.ResetPassWord(id);
                //为true重置密码成功
                return true;
            }
            catch
            {
                return false;
            }
        }
        //编辑学生
        [HttpPost]
        [Route("api/editstudent")]
        public void EditStudent([FromBody] JObject data)
        {

            string studentName = data["studentName"].ToString();
            int sex = Convert.ToInt32(data["sex"]);
            bool isFreeze = false;
            string studentNumber = data["studentNumber"].ToString();
            string passWord = "123456";
            int classID = (int)data["classID"];
            int id = (int)data["id"];
            StudentService.EditStudent(id, studentName, sex, isFreeze, studentNumber, passWord, classID);
        }
    }
}
